﻿Imports System.IO

Public Class Web

    Private arrParameter As ArrayList
    Private strURL As String = Nothing
    Private bHTTPS As Boolean = False

    Public ReadOnly Property URL() As String
        Get
            If bHTTPS Then
                Return "HTTPS://" + strURL + GetParameter()
            Else
                Return "HTTP://" + strURL + GetParameter()
            End If
        End Get
    End Property

    Public Sub New(ByVal Service_URL As String, Optional ByVal HTTPS As Boolean = False)
        strURL = Service_URL
        bHTTPS = HTTPS
        arrParameter = New ArrayList
    End Sub

    Public Sub AddParameter(ByVal Parameter As String, ByVal Value As String, Optional ByVal UrlEncoded As Boolean = False)
        If Not IsNothingOrEmpty(Value) Then
            If UrlEncoded Then
                arrParameter.Add(Parameter + "=" + System.Web.HttpUtility.UrlEncode(Value))
            Else
                arrParameter.Add(Parameter + "=" + Value)
            End If
        End If
    End Sub

    Public Function GetParameter() As String
        Dim strReturn As String = Nothing
        For i As Integer = 0 To arrParameter.Count - 1
            strReturn += "&" + arrParameter(i).ToString
        Next
        Return strReturn.Substring(1, strReturn.Length - 1)
    End Function

    Public Function Request() As String
        Dim responseContent As String = Nothing
        Dim webRequest As System.Net.HttpWebRequest
        Dim webResponse As System.Net.HttpWebResponse
        Try
            webRequest = CType(System.Net.WebRequest.Create(Me.URL), System.Net.HttpWebRequest)
            webResponse = CType(webRequest.GetResponse(), System.Net.HttpWebResponse)
            If Net.HttpStatusCode.OK Then
                Dim responseStream As System.IO.Stream = webResponse.GetResponseStream()
                Dim responseEncoding As System.Text.Encoding = System.Text.Encoding.UTF8
                Dim responseReader As New StreamReader(responseStream, responseEncoding)
                responseContent = responseReader.ReadToEnd()
            End If
        Catch ex As Exception

        End Try
        Return responseContent
    End Function

    Private Function IsNothingOrEmpty(ByVal Value As String) As Boolean
        Dim bReturn As Boolean = False
        If Value = "" Then bReturn = True
        If IsDBNull(Value) Then bReturn = True
        If IsNothing(Value) Then bReturn = True
        Return bReturn
    End Function

End Class
